<template>
  <div>
    <el-form inline size="mini">
      <el-form-item>
        <el-select v-model="company_id" clearable>
          <el-option
            v-for="item in companyList"
            :value="item[0]"
            :key="item[0]"
            :label="item[1]"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="项目名称"
          v-model="projectName"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" class="el-icon-plus" @click="additem"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <Ca-rule-table
      :DataList="approveList"
      :header="header"
      :headle="headle"
      @checkleave="checkitem"
      @delete="deleteitem"
      @modify="print"
      @edit="downfile"
      :columnIndex="8"
      field="manage_status"
      :truestatus="3"
      :progressstatus="1"
    ></Ca-rule-table>
    <paging
      :currentpage="currentpage"
      :currentlimit="currentlimit"
      :total="total"
      @setpage="getpage"
      @setlimit="getlimit"
    ></paging>
    <el-dialog title="合同信息" :visible.sync="isopen" v-dialogDrag top="8vh">
      <add-contract-approve
        :history="history"
        :openType="openType"
        v-if="isopen"
        :contractapprove="contractapprove"
        @closewin="closewin"
      ></add-contract-approve>
    </el-dialog>
    <div v-if="isprint" class="print_table">
      <contract-print
        v-if="isprint"
        :setform="setform"
        :Approvaltable="Approvaltable"
      ></contract-print>
    </div>
  </div>
</template>

<script>
import contractPrint from "./components/contract-print";
import addContractApprove from "@/components/Ca-to-do/add-contract-approve";
import { changetime } from "@/components/global-fn/global-fn";
import {
  apicontractapprove,
  apideleteContract,
  apicontractapproveNew
} from "@/request/api.js";
import { isNumber } from "util";
export default {
  name: "contractApprove",
  data() {
    return {
      isprint: false,
      setform: {},
      Approvaltable: [],
      isopen: false,
      currentlimit: 15,
      currentpage: 1,
      total: 0,
      company_id: "",
      companyList: [
        ["1", "建设公司"],
        ["2", "科技公司"],
        ["3", "加盟合作"],
        ["4", "教育公司"],
        ["5", "传诚管理"]
      ],
      projectName: "",
      approveList: [],
      header: [
        ["项目名称", "manage_contractapprove_name", 90],
        ["发包方（甲方）", "manage_contractapprove_firstParty", 120],
        ["项目地址", "manage_contractapprove_address", 100],
        ["合同开始时间", "manage_contractapprove_startTime", 100],
        ["合同结束时间", "manage_contractapprove_endTime", 100],
        ["合同金额", "manage_contractapprove_amount", 100],
        ["附件地址", "manage_contractapprove_attachAddress", 100, true],
        ["备注", "manage_contractapprove_remark", 100, true],
        ["状态", "manage_status2", 80]
      ],
      headle: ["查看", "删除", "下载", "打印"],
      contractapprove: {},
      history: []
    };
  },
  created() {
    this.getContractApprove();
  },
  components: {
    addContractApprove,
    contractPrint
  },
  methods: {
    closewin() {
      this.isopen = false;
      this.getContractApprove();
    },

    additem() {
      this.history = [];
      this.openType = "add";
      this.contractapprove = {
        userid: "",
        manage_contractapprove_name: "",
        manage_contractapprove_company: "",
        manage_contractapprove_address: "",
        manage_contractapprove_firstParty: "",
        manage_contractapprove_amount: "",
        manage_contractapprove_secondParty: "",
        manage_contractapprove_startTime: "",
        manage_contractapprove_endTime: "",
        manage_contractapprove_payment: "",
        category: 1,
        manage_contractapprove_remark: "",
        manage_contractapprove_taxIncluded: 1,
        manage_contractapprove_report: "1",
        manage_contractapprove_content: [1]
      };
      this.isopen = true;
    },
    query() {
      this.currentlimit = 15;
      this.currentpage = 1;
      this.getContractApprove();
    },
    getlimit(e) {
      this.currentlimit = e;
      this.getContractApprove();
    },
    getpage(e) {
      this.currentpage = e;
      this.getContractApprove();
    },
    print(row) {
      apicontractapproveNew({
        manage_contractapprove_id: row.manage_contractapprove_id
      }).then(res => {
        console.log(res);
        this.setform = res.contractapprove;
        this.setform.manage_contractapprove_company = this.companyList[
          this.setform.manage_contractapprove_company - 1
        ][1];
        this.setform.manage_contractapprove_taxIncluded =
          this.setform.manage_contractapprove_taxIncluded == 1
            ? "含税专票"
            : this.setform.manage_contractapprove_taxIncluded == 2
            ? "含税普票"
            : "不含税";
        this.setform.category =
          this.setform.category == 1
            ? "施工合同"
            : this.setform.category == 2
            ? "供应商合同"
            : "合作合同";
        this.setform.manage_contractapprove_report =
          this.setform.manage_contractapprove_report == "1" ? "是" : "否";
        // 工程内容由字符串转换为数组，-->遍历转换对应内容。-->转换为字符串
        this.setform.manage_contractapprove_content = JSON.parse(
          this.setform.manage_contractapprove_content
        )
          .map(item => {
            switch (item) {
              case 1:
                item = "自动报警系统";
                break;
              case 2:
                item = "防排烟系统";
                break;
              case 3:
                item = "自动喷淋系统";
                break;
              case 4:
                item = "应急照明及疏散指示";
                break;
              case 5:
                item = "配置灭火器";
                break;
            }
            return item;
          })
          .join("、");
        this.Approvaltable = res.history
          ? res.history.map(item => {
              item.START_TIME_ = item.START_TIME_
                ? changetime(item.START_TIME_.time)
                : "";
              item.END_TIME_ = item.END_TIME_
                ? changetime(item.END_TIME_.time)
                : "";
              return item;
            })
          : [];
        this.isprint = true;
        setTimeout(() => {
          this.isprint = false;
        }, 100);
      });
    },
    //查看
    checkitem(row) {
      this.openType = "check";
      apicontractapproveNew({
        manage_contractapprove_id: row.manage_contractapprove_id
      }).then(res => {
        this.contractapprove = res.contractapprove;
        // 当manage_contractapprove_content字段存在时，由字符串转换为数组。
        this.contractapprove.manage_contractapprove_content = this
          .contractapprove.manage_contractapprove_content
          ? JSON.parse(this.contractapprove.manage_contractapprove_content)
          : "";
        // 当manage_contractapprove_content为数字类型时。转换为数组
        this.contractapprove.manage_contractapprove_content = isNumber(
          this.contractapprove.manage_contractapprove_content
        )
          ? [this.contractapprove.manage_contractapprove_content]
          : this.contractapprove.manage_contractapprove_content;
        this.history = res.history
          ? res.history.map(item => {
              item.START_TIME_ = item.START_TIME_
                ? changetime(item.START_TIME_.time)
                : "";
              item.END_TIME_ = item.END_TIME_
                ? changetime(item.END_TIME_.time)
                : "";
              return item;
            })
          : [];
        this.isopen = false;
        this.$nextTick(() => {
          this.isopen = true;
        });
      });
    },
    deleteitem(row) {
      this.$confirm("是否删除？")
        .then(() => {
          apideleteContract({
            manage_contractapprove_id: row.manage_contractapprove_id,
            filePath: row.manage_contractapprove_attachAddress
              ? row.manage_contractapprove_attachAddress
              : ""
          }).then(res => {
            this.$message.success(res.msg);
            this.getContractApprove();
          });
        })
        .catch(() => {});
    },
    downfile(row) {
      console.log(row);
      if (
        row.manage_contractapprove_attachAddress &&
        row.manage_contractapprove_attachAddress !=
          "http://file.casdapi.comnull"
      ) {
        window.open(row.manage_contractapprove_attachAddress);
      } else {
        this.$message.warning("此合同没有附件");
      }
    },
    getContractApprove() {
      let data = {
        limit: this.currentlimit,
        page: this.currentpage,
        manage_contractapprove_name: this.projectName,
        manage_contractapprove_company: this.company_id
      };
      apicontractapprove(data).then(res => {
        console.log(res);
        this.total = res.count;
        this.approveList = res.data.map(item => {
          switch (item.manage_status) {
            case 0:
              item.manage_status2 = "数据录入";
              break;
            case 1:
              item.manage_status2 = "审核中";
              break;
            case 3:
              item.manage_status2 = "审核通过";
              break;
            default:
              item.manage_status2 = "审核不通过";
              break;
          }
          return item;
        });
        console.log(this.approveList);
      });
    }
  }
};
</script>

<style lang="scss">
.print_table {
  position: absolute;
  top: 0;
  background: #fff;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
}
</style>
